package com.itheima.controller;

import com.itheima.interceptors.LoginInterceptor;
import com.itheima.pojo.Password;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.utils.JwtUtil;
import com.itheima.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<User> register(@Pattern(regexp = "^\\S{5,16}$") String username,
                                 @Pattern(regexp = "^\\S{5,16}$") String password) {
        // 查询用户
        User u = userService.findByUserName(username);
        if (u == null) {
            userService.register(username, password);
            return Result.success();
        } else {
            return Result.error("用户名已被占用");
        }
    }

    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username,
                              @Pattern(regexp = "^\\S{5,16}$") String password) {
        User user = userService.login(username);
        if (user == null) {
            return Result.error("用户名都没有呢");
        }

        if (Md5Util.checkPassword(password, user.getPassword())) {
            // 开始发放Jwt令牌啦
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            claims.put("username", user.getUsername());
            String token = JwtUtil.genToken(claims);
            // 结束了，就是这么简单！！！
            return Result.success(token);
        } else {
            return Result.error("用户名是有的，密码错误了呢");
        }
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        // 根据用户名查询用户
        User user = LoginInterceptor.curUser.get();
        return Result.success(user);
    }

    @PutMapping("/update")
    public Result<User> updateUserInfo(@RequestBody User user) {
        // 根据用户信息更新用户
        User u = userService.updateByUserId(user);
        return Result.success(u);
    }

    @PatchMapping("/updateAvatar")
    public Result<String> updateUserAvatar(String avatarUrl) {
        System.out.println(avatarUrl);
        userService.updateCurUserAvatar(avatarUrl);
        return Result.success(avatarUrl);
    }

    @PatchMapping("/updatePwd")
    public Result<String> updatePwd(@RequestBody Password password) {
        String oldPwd  = password.getOldPwd();
        String truePwd = LoginInterceptor.curUser.get().getPassword();

        // 还真有密码，并且还是md5加密过的
        if (!Md5Util.checkPassword(oldPwd, truePwd)) {
            return Result.error("原密码错误，禁止修改");
        }

        String newPwd = password.getNewPwd();
        if (!Objects.equals(newPwd, password.getRePwd())) {
            return Result.error("两次输入的新密码不一致");
        }
        userService.updateCurUserPassword(newPwd);

        return Result.success();
    }

}
